<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="../baselayout/import.jsp"%>
<%@ taglib uri="http://www.bjtu.edu.cn/taglibs" prefix="wt"%>

<wt:table dataSource="student" extendColName="操作">
<button onclick="modUser(this)">修改</button>
</wt:table>
<script>
function modUser(btn){
	var tds = $(btn).parents("tr").children("td").not(":last");
	var tdvals = tds.get().map(function(x){
		return x.innerHTML;
	});
	$.each(tds,function(idx,obj){
		if(idx >= 2 && idx != 5 && idx != 6){
		    $(obj).empty();
		    $(obj).append($("<input type='text' value='"+tdvals[idx]+"'>"));
		}else{
			$(obj).append($("<input type='text' style='display:none' value='"+tdvals[idx]+"'>"));
		}
	});

	function Student(id,name,email,num,type,reg,auth,lower,upper){

		this.userid = id;
		this.name = name;
		this.email=email;
		this.phone=num;
		this.identity = type;
		this.time = reg;
		this.authgroup=auth;

		this.lower = lower;
		this.upper = upper;

		return this;
	} 
	//old student
	var st = Student.apply({},tds);
	function yes(){
		var td =  $(btn).parents("tr").children("td").not(':last');
		var inputs = td.children("input");
		var tdvals = inputs.get().map(function(x){
			return x.value;
		});
		var newst = Student.apply({},tdvals);
		$.each(td,function(x,obj){
			obj.innerText = tdvals[x];
		});
		$(btn).unbind('click');
		$(btn).attr('onclick','modUser(this)').html('修改');
		console.log('修改后的studuent');
		//发送服务器
		console.log(newst);
		$.ajax({
			url : "modUser.do",
			type : "POST",
			datatype : "json",
			data : {
				newst : JSON.stringify(newst)
			},
			success : function(data){
				var obj = JSON.parse(data);
				if(obj.code == 400){
					alert("修改失败");
					refreshSelf();
				}
			}
		});
	}
	$(btn).attr('onclick','').html('确定');
	$(btn).click(yes);
	
}
</script>